package com.iwdnb.gkgz.common.prediction;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

import cn.hutool.core.date.DateUtil;
import com.iwdnb.gkgz.common.model.dto.StockDayData;
import com.iwdnb.gkgz.common.model.dto.StockPredictionData;

/**
 * 明日高低点预测公式
 * 阳线基值R=(最高价*2+最低价+收盘价)/2；
 * 阴线基值R=(最低价*2+最高价+收盘价)/2；
 * 十字星基值R=(收盘价*2+最高价+最低价)/2.
 * 共有价为最高价+最低价+收盘价
 * 明日预测高价=R-最低价 明日预测低价=R-最高价
 */
public class ThirdPredictionHelper implements PredictionHelper {
    @Override
    public StockPredictionData predictionStock(StockDayData stockDayData) {
        BigDecimal averageTotal = getAverageTotal(stockDayData);
        StockPredictionData prediction = new StockPredictionData();
        prediction.setCode(stockDayData.getCode());
        Date date = DateUtil.offsetDay(DateUtil.parseDate(stockDayData.getDate()), 1);
        prediction.setDate(DateUtil.formatDate(date));
        prediction.setMaxPrice(averageTotal.subtract(stockDayData.getMinPrice())
            .setScale(2, RoundingMode.HALF_DOWN));
        prediction.setMinPrice(averageTotal.subtract(stockDayData.getMaxPrice())
            .add(new BigDecimal(0.005))
            .setScale(2, RoundingMode.HALF_UP));
        return prediction;
    }

    private BigDecimal getAverageTotal(StockDayData stockDayData) {
        BigDecimal total = stockDayData.getClosePrice()
            .add(stockDayData.getMaxPrice())
            .add(stockDayData.getMinPrice());

        int compare = stockDayData.getOpenPrice().compareTo(stockDayData.getClosePrice());
        if (compare == 0) {
            //十字星
            return total.add(stockDayData.getClosePrice()).divide(new BigDecimal(2));
        } else if (compare < 0) {
            //阳线
            return total.add(stockDayData.getOpenPrice()).add(new BigDecimal(0.003)).divide(new BigDecimal(2));
        } else {
            return total.add(stockDayData.getMinPrice()).subtract(new BigDecimal(0.003)).divide(new BigDecimal(2));
        }
    }
}
